package icu.twtool.ui.router.routes.practice

import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.width
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import icu.twtool.constant.parse
import icu.twtool.database.vo.PracticeExerciseRecordVO
import icu.twtool.entity.BinaryEquation
import icu.twtool.entity.Exercise
import icu.twtool.entity.Practice
import icu.twtool.ui.component.Table
import icu.twtool.ui.component.TableProps
import icu.twtool.ui.component.TertiaryTextButton
import icu.twtool.ui.style.MyColors
import icu.twtool.util.PathUtil

/**
 * @author Wen
 * @since 2022-11-05
 */
@Composable
@Suppress("FunctionName")
fun PracticeRecordTable(data: List<PracticeExerciseRecordVO>) {
    var lookPracticeDialogData by remember { mutableStateOf<PracticeExerciseRecordVO?>(null) }
    Table(
        data,
        arrayOf(
            TableProps("序号", { Modifier.width(96.dp).fillMaxHeight() }) { Text(it.record.id.toString()) },
            TableProps("习题", { Modifier.weight(1f).fillMaxHeight() }) { Text(it.name) },
            TableProps("成绩", { Modifier.width(96.dp).fillMaxHeight() }) { Text((100 * it.record.rightCount / it.count).toString()) },
            TableProps("练习时间", { Modifier.width(200.dp).fillMaxHeight() }) { Text(it.record.practiceTime.parse()) },
            TableProps("操作", { Modifier.width(200.dp).fillMaxHeight() }) {
                TertiaryTextButton("查看") {
                    lookPracticeDialogData = it
                }
            }
        )
    )
    lookPracticeDialogData?.let {  vo ->
        var exercise: Exercise? = null
        var practice: Practice? = null
        var flag = true
        try {
            exercise = Exercise.readFile(PathUtil.exercise().resolve(vo.reallyName))
            practice = Practice.readFile(PathUtil.practice().resolve(vo.record.practiceFile))
        } catch (_: Exception) { }
        Dialog(onCloseRequest = { lookPracticeDialogData = null }, title = "答案对比") {
            if (exercise != null && practice != null) {
                val exerciseSize = exercise.size
                val practiceSize = practice.size
                if (exerciseSize == practiceSize) {
                    flag = false
                    val epData = mutableListOf<Pair<BinaryEquation, Int>>()
                    repeat(exerciseSize) { index ->
                        epData.add(exercise.get(index) to practice[index])
                    }
                    Table(epData, arrayOf(
                        TableProps("算式", { Modifier.weight(2f).fillMaxHeight() }) { Text(it.first.toString(false)) },
                        TableProps("正确答案", { Modifier.weight(1f).fillMaxHeight() }) { Text(it.first.answer().toString()) },
                        TableProps("你的答案", { Modifier.weight(1f).fillMaxHeight() }) { Text(it.second.toString(), color = if (it.first.answer() != it.second) MyColors.Error else MyColors.Success) },
                    ))
                }
            }
            if (flag) {
                Text("文件损坏")
            }
        }
    }
}